import { getToPathname } from "@remix-run/router"
import axios from "axios"// 引入axios请求
import { createBrowserHistory } from "history"
let history = createBrowserHistory()
let http = axios.create({
  baseURL: "/api",
  withCredentials: true,
  timeout:5*1000,
})

// 请求拦截器
http.interceptors.request.use(config => {
  // 获取token
  let token = JSON.parse(localStorage.getItem('tq-token') as string)
  config.headers!.authorization = token
  return config
})

// 响应拦截器
http.interceptors.response.use(config => {
  // console.log(config)
  if (config.data.code === "1004") {
    alert('登录失败,请重新登录')
    localStorage.removeItem('tq-token')
    history.push('/login')
    window.location.reload()
  }
  return config
})


export default http